package com.fyzk.adapter;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.fyzk.activity.app.AppToast;
import com.fyzk.activity.lottery.jc.football.newJz.data.NewScoreDetailActivity;
import com.fyzk.bean.JCLQResponseInfo;
import com.fyzk.model.JCLQMatchData;
import com.tyjc.lottery.R;

import java.text.SimpleDateFormat;
import java.util.List;


/**
 * 让分胜负adapter
 *
 * @author fxs
 */
public class JCLQRSFAdapter extends JCLQBaseAdapter {

    public JCLQRSFAdapter(Activity activity, List<JCLQResponseInfo> infos) {
        super(activity, infos);
    }

    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        final JCLQMatchData data = infos.get(groupPosition).matchDatas
                .get(childPosition);
        final ChildHolder childHolder;
        if (convertView == null) {
            convertView = miInflater.inflate(R.layout.jclq_adapter_child_rsf,
                    null);
            childHolder = new ChildHolder();
            childHolder.xianShi = convertView.findViewById(R.id
                    .jc_main_list_item_xianshi);
            childHolder.ll_xiangqing = convertView.findViewById(R.id
                    .jc_main_list_item_yincang);
            childHolder.league_tv = convertView
                    .findViewById(R.id.jclq_child_base_league);
            childHolder.teamId_tv = convertView
                    .findViewById(R.id.jclq_child_base_teamid);
            childHolder.time_tv = convertView
                    .findViewById(R.id.jclq_child_base_time);
            childHolder.right_ll = convertView
                    .findViewById(R.id.jclq_child_base_right);
            childHolder.home_check = convertView
                    .findViewById(R.id.jclq_child_rsf_home);
            childHolder.away_check = convertView
                    .findViewById(R.id.jclq_child_rsf_away);
            childHolder.rangFen_check = convertView
                    .findViewById(R.id.jclq_child_rsf_rfsf);
            childHolder.home_name = convertView
                    .findViewById(R.id.jclq_child_rsf_home_name);
            childHolder.away_name = convertView
                    .findViewById(R.id.jclq_child_rsf_away_name);

            convertView.setTag(childHolder);
        } else {
            childHolder = (ChildHolder) convertView.getTag();
        }

        childHolder.league_tv.setText(data.getMatchName());
        try {
            String teamId = data.getMatchNum().substring(data.getMatchNum().length() - 3, data
                    .getMatchNum().length());
            childHolder.teamId_tv.setText(data.getOfficialWeekDay() + teamId);
            String endData = new SimpleDateFormat("HH:mm").format(data.getEndSaleTime());
            childHolder.time_tv.setText(endData + "截止");
        } catch (Exception e) {

        }

        childHolder.rangFen_check.setChecked(data.getHandicap().contains("+"));
        childHolder.rangFen_check.setText("主" + data.getHandicap());
        // 初始化选中状态前不让checkbox响应事件
        childHolder.home_check.setOnCheckedChangeListener(null);
        childHolder.away_check.setOnCheckedChangeListener(null);

        JCLQMatchData.SPInfo spInfo_ls = data.splInfos.get(data.LETVS_3);
        JCLQMatchData.SPInfo spInfo_lf = data.splInfos.get(data.LETVS_0);

        childHolder.home_check.setChecked(spInfo_ls.isSel);
        childHolder.away_check.setChecked(spInfo_lf.isSel);

        childHolder.away_name.setText(data.getAwayTeam() + "[客]");
        childHolder.home_name.setText(data.getHomeTeam() + "[主]");
        childHolder.home_check.setText("主胜 " + spInfo_ls.spValue);
        childHolder.away_check.setText("客胜 " + spInfo_lf.spValue);

        childHolder.home_check.setTag(R.string.jc_info, data);
        childHolder.home_check.setTag(spInfo_ls);

        childHolder.away_check.setTag(R.string.jc_info, data);
        childHolder.away_check.setTag(spInfo_lf);

        childHolder.home_check.setOnClickListener(clickListener);


        childHolder.away_check.setOnClickListener(clickListener);

        if (data.isShowHideView) {
            childHolder.ll_xiangqing.setVisibility(View.VISIBLE);
        } else {
            childHolder.ll_xiangqing.setVisibility(View.GONE);
        }
        if (data.hideView != null) {

            ViewGroup parent1 = (ViewGroup) data.hideView.getParent();
            if (parent1 != null) {
                parent1.removeAllViews();
            }
            childHolder.ll_xiangqing.removeAllViews();
            childHolder.ll_xiangqing.addView(data.hideView);
        }
        // 点击显示隐藏信息
        childHolder.xianShi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

//                if (data.hideView == null) {
//                    data.hideView = new BasketballMatchSummaryView(context, data.getMatchNum());
//                    childHolder.ll_xiangqing.removeAllViews();
//                    childHolder.ll_xiangqing.addView(data.hideView);
//                }
//                data.isShowHideView = !data.isShowHideView;
//                if (data.isShowHideView) {
//                    childHolder.ll_xiangqing.setVisibility(View.VISIBLE);
//                } else {
//                    childHolder.ll_xiangqing.setVisibility(View.GONE);
//                }
                Intent intent = new Intent(context, NewScoreDetailActivity.class);
                intent.putExtra("isFootball", false);
                intent.putExtra("matchNum", data.getMatchNum());
                context.startActivity(intent);
            }

        });
//        childHolder.ll_xiangqing.setOnClickListener(new View.OnClickListener() {
//
//            @Override
//            public void onClick(View v) {
//                Intent intent = new Intent(context, BasketballScoreDetailActivity.class);
//                intent.putExtra("matchNum", data.getMatchNum());
//                context.startActivity(intent);
//            }
//        });
        return convertView;
    }

    View.OnClickListener clickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            JCLQMatchData.SPInfo spInfo = (JCLQMatchData.SPInfo) v.getTag();
            JCLQMatchData data = (JCLQMatchData) v.getTag(R.string.jc_info);
            spInfo.isSel = !spInfo.isSel;
            int clickNum = data.clickNum;
            if (spInfo.isSel) {
                data.clickNum++;
            } else {
                data.clickNum--;
            }
            if (spInfo.isSel && changCi == maxCount && data.clickNum == 1) {
                AppToast.getToast().show("最多选择" + maxCount + "场");
                spInfo.isSel = false;
                data.clickNum = clickNum;
                ((CheckBox) v).setChecked(false);
                return;
            }
            tongJi();
        }
    };

    class ChildHolder {
        TextView league_tv;
        TextView teamId_tv;
        TextView time_tv;
        LinearLayout right_ll;
        CheckBox home_check;
        CheckBox away_check;
        CheckBox rangFen_check;
        TextView home_name;
        TextView away_name;
        LinearLayout xianShi;
        LinearLayout ll_xiangqing;
    }

}
